package com.tarry.threadpool;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class CacheData {

    private Integer number = 0;

    private final ReadWriteLock lock = new ReentrantReadWriteLock();

    public void getNumber() {
        lock.readLock().lock();
        try {
            System.out.printf("当前线程【%s】,读取数据为： %d \n",Thread.currentThread().getName(),number);
        }finally {
            lock.readLock().unlock();
        }
    }

    public void setNumber(Integer number) {
        lock.writeLock().lock();
        try {
            this.number = number;
            System.out.printf("当前线程【%s】,写入数据为： %d \n", Thread.currentThread().getName(),number);
        }finally {
            lock.writeLock().unlock();
        }
    }

}
